﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FMSpaceShooter.Screens;
using Microsoft.Xna.Framework;
using FMSpaceShooter.GameObjects.ItemFolder;

namespace FMSpaceShooter.GameObjects
{
    class OrbitalEnemy : EnemyBase
    {
        public new const string TypeName = "OrbitalEnemy";
        public const string imageName = "Images/Enemies/GreenSpinner";
        float rot;
        float revolutions;
        public override int Points { get { return 40; } }



        public OrbitalEnemy(GameplayScreen screen, Vector2 position)
            :base(screen, imageName, position, 400)
        {
            rot = 0;
            revolutions = 0;
        }



        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Vector2 direction;
            if (Vector2.Distance(screen.GOM.PlayerOne.Position, position) >300)
            {
                direction = screen.GOM.PlayerOne.Position - position;
                direction.Normalize();
                position += (direction * (float)(gameTime.ElapsedGameTime.TotalSeconds * 100)*2);
                rotation=(float)Math.Atan2(direction.X, -direction.Y);
                rot = (float)Math.Atan2(direction.Y, direction.X) + (float)Math.PI*3 / 2;
                


            }
            else 
            {
                int speed = 3;
                float r = Vector2.Distance(screen.GOM.PlayerOne.Position, position);
                float c = r / ((float)gameTime.ElapsedGameTime.TotalSeconds*100*speed* (float)Math.PI);
                rot += 1 / (float)(Math.PI * c);
                direction = new Vector2( (float)Math.Cos(rot) ,  (float)Math.Sin(rot));
                direction.Normalize();
                position += (direction * (float)(gameTime.ElapsedGameTime.TotalSeconds*100))*speed;
                revolutions += (float).1;
                rotation += (float)Math.Atan2(position.X,position.Y);


                if (revolutions > 1)
                {
                    revolutions = 0;
                    Vector2 playerLocation = screen.GOM.PlayerOne.Position - position;
                    playerLocation.Normalize();
                    position += (playerLocation * (float)(gameTime.ElapsedGameTime.TotalSeconds * 100));
                }
                

            }

            
        }


        public override void Die()
        {
            base.Die();
            int x = screen.Random.Next(20);

            if (x < 3)
            {
                screen.GOM.AddToPlay(new PrimaryBulletBoost(screen, this.position));
            }
            else if (x>17)
            {
                screen.GOM.AddToPlay(new SplitterBulletBoost(screen, this.position));
            }
            else if (x == 5)
            {
                screen.GOM.AddToPlay(new ExplosionBulletBoost(screen, this.position));
            }

        }


    }
}
